﻿using System;
using System.Diagnostics;
using TI_BLE_HCI_ClientLib.HCIBaseEntities;
using TI_BLE_HCI_ClientLib.HCIEvents;
using Utilities;

namespace TI_BLE_HCI_ClientLib.HCIParsers
{
    public static partial class HCIEventParser
    {
        private static HCIEvent parseHCI_LE_ExtEvent_GAP_Device_Discovery(byte parsLen, byte[] buffer)
        {
            if (parsLen < 2) throw new ArgumentException("parseHCI_LE_ExtEvent_GAP_DeviceDiscovery Parameters Length < 2");
            byte bufIdx = 4;

            HCIVendorSpecificEventHostErrorCode status = (HCIVendorSpecificEventHostErrorCode)parseByteParameter(buffer, ref bufIdx);

            var hciEvent = new HCIEvent_GAP_Device_Discovery_Done(status);

            byte numDevs = parseByteParameter(buffer, ref bufIdx);

            for (var i = 0; i < numDevs; i++)
            {
                HCIEvent_GAP_Device_Discovery_Done.Device.EventTypeEnum eventType = (HCIEvent_GAP_Device_Discovery_Done.Device.EventTypeEnum)parseByteParameter(buffer, ref bufIdx);
                HCIEvent_GAP_Device_Discovery_Done.Device.AddrTypeEnum addrType = (HCIEvent_GAP_Device_Discovery_Done.Device.AddrTypeEnum)parseByteParameter(buffer, ref bufIdx);
                byte[] addr = parseBufferParameter(6, buffer, ref bufIdx);

                hciEvent.addDevice(new HCIEvent_GAP_Device_Discovery_Done.Device(eventType, addrType, addr));
            }

            return hciEvent;
        }
    }
}
